<template>
    <div class="said">
        <div class="bg">
            <div class="resgiter">用户注册</div>
            <div class="sum">
                <a-form
                    ref="formRef"
                    :model="formState"
                    name="basic"
                    :label-col="{ span: 8 }"
                    :wrapper-col="{ span: 16 }"
                    autocomplete="off"
                    @finish="onFinish"
                    @finishFailed="onFinishFailed"
                >
                    <a-form-item
                        :label="setcolor('用户名')"
                        class="formcenter"
                        name="username"
                        :rules="[{ required: true, message: '请输入用户名！' }]"
                    >
                        <a-input v-model:value="formState.username" />
                    </a-form-item>

                    <a-form-item
                        class="formcenter"
                        :label="setcolor('密码')"
                        name="password"
                        :rules="[{ required: true, message: '请输入密码！' }]"
                    >
                        <a-input-password v-model:value="formState.password" />
                    </a-form-item>

                    <a-form-item name="remember" :wrapper-col="{ offset: 18, span: 16 }">
                        <a href="/login" class="a">已注册，去登录</a>
                    </a-form-item>

                    <a-form-item :wrapper-col="{ offset: 8, span: 16 }">
                        <div class="submit">
                            <a-button
                                type="primary"
                                :loading="isLoading"
                                html-type="submit"
                                @click="toLogin"
                                >注册</a-button
                            >
                        </div>
                    </a-form-item>
                </a-form>
            </div>
        </div>
    </div>
</template>

<script setup>
import { reactive } from 'vue';
import { useRouter } from 'vue-router';

import { message } from 'ant-design-vue';

import service from '@/service';

// 自定义渲染label

const setcolor = (name) => {
    return h('span', { style: { color: 'white' } }, name);
};
// 登录接口请求
const isLoading = ref(false);
const router = useRouter();
const formRef = ref(null);
const formState = reactive({
    username: '',
    password: '',
});

const onFinish = (values) => {
    formState.username = values.username;
    formState.password = values.password;
};
const onFinishFailed = (errorInfo) => {
    message.error(errorInfo);
};
// 登录板块

async function toLogin() {
    try {
        const params = {
            username: formState.username,
            password: formState.password,
        };
        const res = await service.post('/user', params);

        isLoading.value = true;

        if (res === 'success') {
            message.success('注册成功！');

            router.push({ path: '/login' });
        } else {
            message.error('注册失败,请重新注册');
        }
    } catch (error) {
        message.error('注册失败,请重新注册');
    }
}
</script>

<style lang="scss" scoped>
.said {
    width: 100%; //大小设置为100%
    height: 100%; //大小设置为100%
    position: fixed;

    background: url('@/assets/bg1.png');
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
}
@keyframes xx {
    0% {
        background-position: 0% 50%;
    }
    50% {
        background-position: 100% 50%;
    }
    100% {
        background-position: 0% 50%;
    }
}
.bg {
    display: flex;
    flex-direction: column;
    justify-content: center;
    width: 600px;
    height: 400px;
    background: rgba(0, 0, 86, 0.6);
    border-radius: 5px;
    border: 2px solid rgba(0, 177, 255, 0.9);
    margin-top: 80px;

    margin-left: 20px;
}
.resgiter {
    font-size: 30px;
    color: #fff;
    text-align: center;
    height: 35px;
    font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
    margin-top: 20px;
}
.sum {
    color: #fff;
    width: 400px;
    height: 200px;

    margin-left: 70px;
    margin-top: 50px;
}
.sub {
    margin-left: 100px;
}
.submit {
    width: 70px;
    height: 30px;
    margin-left: 26%;
    color: #fff;
    cursor: pointer;
}
.formcenter {
    padding: 0;
    margin-left: -40px;
}
.a {
    color: #fff;
}
</style>
